package com.hyc.myvideo.ui.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.constants.Constants;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Code;
import com.hyc.myvideo.data.api.model.Coupon;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.widget.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

/**
 * 优惠券转赠
 * @author gyb
 * @version 1.0
 * @created 2017/07/20
 */
public class CouponDonatedActivity extends BaseFActivity {
    //展示当前优惠券总是
    @BindView(R.id.coupon_t_number)
    TextView textViewNumber;
    //转赠账号
    @BindView(R.id.coupon_ed_phone)
    ClearEditText editTextPhone;
    //转赠数量
    @BindView(R.id.coupon_ed_number)
    ClearEditText editTextNumber;
    /**确定按钮*/
    @BindView(R.id.coupon_b_determine)
    Button buttonSubmit;
    /**转赠优惠卷对象*/
    private Coupon coupon = null;
    @Override
    public int getContentViewId() {
        return R.layout.activity_coupon_donated;
    }

    @Override
    protected void initView() {
        setButtonSubmit(false);
        editTextPhone.addTextChangedListener(textWatcherDonated);
        editTextNumber.addTextChangedListener(textWatcherNumber);
        Bundle extras = getIntent().getExtras();
        try {
            setLeftBtnClick(true,extras.getString("title"));
            coupon = (Coupon) extras.get("coupon");
            Timber.i("============coupon=================="+coupon.toString());
            String number = String.format(appContext.getString(R.string.huc_coupon),coupon.getGiev_count()+"");
            textViewNumber.setText(number);
        }catch(Exception e){
            Timber.e(e,"优惠券转赠传值异常");
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void oNext(String r, String method) {
        BaseResult<Code> result = JSONObject.parseObject(r, new TypeReference<BaseResult<Code>>(){});
        if(result.isCode()){
            setCallback();
        }
        Toaster.show(result.getMsg());
    }

    @Override
    protected void oError() {

    }

    /***
     * 转赠成功回调
     */
    private void setCallback() {
        setResult(Constants.RESULT_CODE, new Intent());
        this.finish();
    }


    @OnClick({
            R.id.coupon_b_cancel,R.id.coupon_b_determine,
    })
    public void onClicks(View view) {
        switch (view.getId()){
            /**取消*/
            case R.id.coupon_b_cancel:
                 finish();
                break;
            /**确定*/
            case R.id.coupon_b_determine:
                if(verification()){
                    api.requestCouponDonated(coupon.getUser_bill_id(),getBEDstr(editTextPhone),getBEDstr(editTextNumber));
                }
                break;
        }
    }

    /***
     * 验证转赠信息
     * @return
     */
    private boolean verification(){
        boolean isv = false;
        int number = Integer.parseInt(getBEDstr(editTextNumber));
        if(number > coupon.getGiev_count()){
            Toaster.show("转赠数量错误,请重新填写");
            return isv;
        }
        isv = true;
        return isv;
    }

    /***
     * 监听转赠手机号
     */
    TextWatcher textWatcherDonated = new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override public void afterTextChanged(Editable s) {
            int num = s.toString().length();
            if(num >= 11 && getBEDstr(editTextNumber).length() > 0){
                setButtonSubmit(true);
            }else {
                setButtonSubmit(false);
            }
        }
    };

    /***
     * 监听转赠数量
     */
    TextWatcher textWatcherNumber= new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override public void afterTextChanged(Editable s) {
            int num = s.toString().length();
            if(num > 0 && getBEDstr(editTextPhone).length() >= 11){
                setButtonSubmit(true);
            }else{
                setButtonSubmit(false);
            }

        }
    };

    /***
     * 设置提交按钮状态
     * @param isClickable
     */
    private void setButtonSubmit(boolean isClickable){
        buttonSubmit.setClickable(isClickable);
        if(isClickable){
            buttonSubmit.setBackgroundResource(R.drawable.button_shape_1);
        }else{
            buttonSubmit.setBackgroundResource(R.drawable.button_shape_0);
        }
    }

}
